package com.laikan.reader.core.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

public class AppUtil {

    /**
     * 获取版本号
     *
     * @param context
     *
     * @return
     */
    public static int getAppVersionCode(Context context) {
        int versonCode = 1;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versonCode = pi.versionCode;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return versonCode;
    }

    /**
     * 获取包名
     *
     * @param cxt
     *
     * @return
     */
    private static String getPkgName(Context cxt) {
        PackageManager pManager = cxt.getPackageManager();
        PackageInfo pkgInfo = null;
        try {
            pkgInfo = pManager.getPackageInfo(cxt.getPackageName(), 0);
        }catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return pkgInfo.packageName;
    }

    /**
     * 卸载指定应用包
     *
     * @param cxt
     */
    public static void uninstallApp(Context cxt) {
        String pkgName = getPkgName(cxt);
        Uri data = Uri.fromParts("package", pkgName, null);
        Intent intent = new Intent(Intent.ACTION_DELETE, data);
        cxt.startActivity(intent);
    }

}
